﻿using NPOI.HSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Serialization;

namespace Alimentador.Foods
{
    [Serializable]
    public class FoodCategory : FoodBase
    {
        /// <summary>
        /// List of Foods for children objects
        /// </summary>
        [XmlElement("Foods")]
        public ObservableCollection<Food> Foods { get; set; }
        
        /// <summary>
        /// Constructor
        /// </summary>
        public FoodCategory()
        {
            this.Foods = new ObservableCollection<Food>();
        }

        /// <summary>
        /// Load the property list from a given HSSFSheet into the food
        /// </summary>
        /// <param name="sheet">HSSFSheet</param>
        /// <returns>bool</returns>
        public static Food LoadFoodFromFHSSFSheet(HSSFSheet sheet)
        {
            Food food = new Food();
            try
            {
                foreach (PropertyName prop in Enum.GetValues(typeof(PropertyName)))
                {
                    FoodProperty fprop = new FoodProperty();

                    HSSFRow dataRow = sheet.GetRow((int)prop);
                    if (prop == PropertyName.Ficha)
                    {
                        food.GetName = dataRow.GetCell(1).ToString();
                        food.GetItemType = FoodBase.ItemType.Food;
                    }
                    else
                    {
                        if (dataRow.GetCell(1) != null)
                        {
                            double val = double.Parse(dataRow.GetCell(1).ToString());
                            fprop = new FoodProperty(prop, val);
                            food.PropertyList.Add(fprop);
                        }
                    }
                }

                return food;
            }
            catch (Exception)
            {
                return food;
            }
        }

        /// <summary>
        /// Load the foods for the given file list. Returns the list string of file names that were not loaded.
        /// </summary>
        /// <param name="foodName">string</param>
        /// <param name="fileNameList">IEnumerable(string)</param>
        /// <param name="sheetName">string</param>
        /// <param name="maxRows">int</param>
        /// <param name="maxColumns">int</param>
        /// <returns>List(string)</returns>
        public List<string> LoadFoodListFromFiles(IEnumerable<string> fileNameList, string sheetName = "Ficha ", int maxRows = 51, int maxColumns = 3)
        {
            List<string> errorList = new List<string>();

            this.Foods = new ObservableCollection<Food>();

            foreach (string fileName in fileNameList)
            {
                try
                {
                    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                    HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true);
                    HSSFSheet sheet = templateWorkbook.GetSheet(sheetName);

                    if (sheet != null)
                    {
                        Food food = FoodCategory.LoadFoodFromFHSSFSheet(sheet);
                        if (food != null)
                        {
                            this.Foods.Add(food);
                        }
                    }
                    fs.Close();
                }
                catch
                {
                    errorList.Add(fileName);
                }
            }
            return errorList;
        }
    }
}
